#include "rtc.h"
#include "gd32e23x_it.h"
#include "config.h"
#include "mcu_api.h"
#include "wifi.h"

#define RTC_CLOCK_SOURCE_IRC40K 

uint8_t rtc_init_flag;


void rtc_show_time(void)
{
   uint8_t year,mouth, date,hour,minute,second;
   uint8_t t1,t2;
   rtc_parameter_struct rtc_initpara;
   rtc_current_time_get(&rtc_initpara);
   gd_printf(("----get rtc time:year=0x%x,mouth=0x%x,date=0x%x,hour=0x%x,minute=0x%x,second=0x%x\r\n",\
   rtc_initpara.rtc_year,rtc_initpara.rtc_month,rtc_initpara.rtc_date,rtc_initpara.rtc_hour,rtc_initpara.rtc_minute,rtc_initpara.rtc_second));
   /*t1 = (rtc_initpara.rtc_year>>4&0xf)*10;
   t2 = rtc_initpara.rtc_year&0xf;
   year = t1+t2;
   t1 = (rtc_initpara.rtc_month>>4&0xf)*10;
   t2 = rtc_initpara.rtc_month&0xf;
   mouth = t1+t2;
   t1 = (rtc_initpara.rtc_date>>4&0xf)*10;
   t2 = rtc_initpara.rtc_date&0xf;
   date = t1+t2;
   t1 = (rtc_initpara.rtc_hour>>4&0xf)*10;
   t2 = rtc_initpara.rtc_hour&0xf;
   hour = t1+t2;
   t1 = (rtc_initpara.rtc_minute>>4&0xf)*10;
   t2 = rtc_initpara.rtc_minute&0xf;
   minute = t1+t2;
   t1 = (rtc_initpara.rtc_second>>4&0xf)*10;
   t2 = rtc_initpara.rtc_second&0xf;
   second = t1+t2;

   gd_printf(("Current time: %d-%d-%d---%d:%d:%d\r\n", \
         year+2000,mouth,date, \
         hour, minute, second\
         ));*/
}

void rtc_set_time(uint8_t year,uint8_t mouth, uint8_t date,uint8_t hour,uint8_t minute, uint8_t second,uint8_t week)
{
   rtc_parameter_struct rtc_initpara;
   uint8_t tmp1,tmp2;
   rtc_init_flag=1;
   tmp1 = year/10;
   tmp2 = year%10;
   rtc_initpara.rtc_year = tmp1<<4|tmp2;

   tmp1 = mouth/10;
   tmp2 = mouth%10;
   rtc_initpara.rtc_month = tmp1<<4|tmp2;
   
   tmp1 = date/10;
   tmp2 = date%10;
   rtc_initpara.rtc_date = tmp1<<4|tmp2;

   tmp1 = hour/10;
   tmp2 = hour%10;
   rtc_initpara.rtc_hour = tmp1<<4|tmp2;

   tmp1 = minute/10;
   tmp2 = minute%10;
   rtc_initpara.rtc_minute = tmp1<<4|tmp2;

   tmp1 = second/10;
   tmp2 = second%10;
   rtc_initpara.rtc_second = tmp1<<4|tmp2;
   
   rtc_initpara.rtc_am_pm = RTC_AM;
   rtc_initpara.rtc_display_format = RTC_24HOUR;
   rtc_initpara.rtc_factor_asyn=0x63;
   rtc_initpara.rtc_factor_syn= 0x18F;
   gd_printf(("cloud give time:year=%d,mouth=%d,date=%d,hour=%d,minute=%d,second=%d,week=0x%x\r\n",\
   year,mouth,date,hour,minute,second,week));

   gd_printf(("----cloud give time HEX:year=0x%x,mouth=0x%x,date=0x%x,hour=0x%x,minute=0x%x,second=0x%x,week=0x%x\r\n",\
   rtc_initpara.rtc_year,rtc_initpara.rtc_month,rtc_initpara.rtc_date,rtc_initpara.rtc_hour,rtc_initpara.rtc_minute,rtc_initpara.rtc_second,week));
   rtc_initpara.rtc_day_of_week=RTC_SUNDAY;
   /* RTC current time configuration */
    if(ERROR == rtc_init(&rtc_initpara))
    {    
        gd_printf(("\n\r** RTC time configuration failed! **\n\r"));
    }
    else
    {
        gd_printf(("\n\r** RTC time configuration success! **\n\r"));
        rtc_show_time();
    }   
}

void rtc_pre_config(void)
{
    #if defined (RTC_CLOCK_SOURCE_IRC40K) 
          rcu_osci_on(RCU_IRC40K);
          rcu_osci_stab_wait(RCU_IRC40K);
          rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  
         // prescaler_s = 0x18F;
         // prescaler_a = 0x63;
    #elif defined (RTC_CLOCK_SOURCE_LXTAL)
          rcu_osci_on(RCU_LXTAL);
          rcu_osci_stab_wait(RCU_LXTAL);
          rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
        //  prescaler_s = 0xFF;
        //  prescaler_a = 0x7F;
    #else
    #error RTC clock source should be defined.
    #endif /* RTC_CLOCK_SOURCE_IRC40K */

    rcu_periph_clock_enable(RCU_RTC);
    rtc_register_sync_wait();
}

void user_rtc_init(void)
{
    /* enable access to RTC registers in Backup domain */
    rcu_periph_clock_enable(RCU_PMU);
    pmu_backup_write_enable();
    rtc_pre_config();
    
    rtc_init_flag = 0;
}

